package com.itheima.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.common.EmployeeHolder;
import com.itheima.reggie.domain.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自定义元数据对象处理器
 * @author zsk
 * @create 2022-03-04-13:04
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    //插入操作，自动填充
    @Override
    public void insertFill(MetaObject metaObject) {

        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime", new Date());
        //从ThreadLocal对象中获取Employee对象
        Employee employee = EmployeeHolder.get();
        if (employee != null){
            metaObject.setValue("createUser", employee.getId());//获取当前用户的id
            metaObject.setValue("updateUser", employee.getId());
        }

    }

    //修改操作，自动填充
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", new Date());
        //从ThreadLocal对象中获取Employee对象
        Employee employee = EmployeeHolder.get();
        if (employee !=null){
            metaObject.setValue("updateUser", employee.getId());
        }
    }
}
